package cn.findviewbydream.evil.main.service.impl;

import cn.findviewbydream.evil.main.dao.CodeDao;
import cn.findviewbydream.evil.main.dto.CodeTypeBean;
import cn.findviewbydream.evil.main.dto.PostBean;
import cn.findviewbydream.evil.main.dto.PostListBean;
import cn.findviewbydream.evil.main.entity.CodeTypeEntity;
import cn.findviewbydream.evil.main.entity.PostEntity;
import cn.findviewbydream.evil.main.entity.PostPraiseEntity;
import cn.findviewbydream.evil.main.entity.UserEntity;
import cn.findviewbydream.evil.main.service.CodeService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/********************************************
 * 工程名：HebeuCS
 * 包名：cn.findviewbydream.evil.main.service.impl
 * 时间：16-11-5 下午1:21
 * 作者：白雨濃 evil
 * 邮箱：evilbai@foxmail.com
 * 内容：代码讨论区实现类
 ********************************************/
@Service("codeService")
public class CodeServiceImpl implements CodeService
{
    private Logger logger = LoggerFactory.getLogger(ReleaseNewpostServiceImpl.class);

    private static final int EVERY_PAGE_LIST_SIZE = 10;/*每页最大贴子数目*/
    private final CodeDao codeDao;

    @Autowired
    public CodeServiceImpl(CodeDao codeDao)
    {
        this.codeDao = codeDao;
    }

    @Override
    public List<CodeTypeBean> getCodeTypeList()
    {
        List<CodeTypeBean> list = new ArrayList<>();
        List<CodeTypeEntity> entityList = codeDao.selectCodeType();
        for (CodeTypeEntity entity : entityList)
        {
            list.add(new CodeTypeBean(entity, codeDao.selectPostCount(entity)));
        }
        return list;
    }

    @Override
    public PostListBean getPostList(HttpSession session, String type, int cpn)
    {
        type = type.replaceAll("-", "/");/*防止/在url出错，改变成-，然后在后台改变回来*/

        PostListBean postListBean = new PostListBean();
        /*贴子列表的代码类型*/
        CodeTypeEntity codeTypeEntity = null;
        {
            codeTypeEntity = codeDao.selectCodeTypeByType(type);
        }
        postListBean.setCodeType(codeTypeEntity);
        /*列表的总页数*/
        int count = 0;
        {
            int itemNum = codeDao.selectPostCount(codeTypeEntity);//贴子总数
            if (itemNum % EVERY_PAGE_LIST_SIZE == 0)
                count = itemNum / EVERY_PAGE_LIST_SIZE;
            else
                count = itemNum / EVERY_PAGE_LIST_SIZE + 1;
        }
        postListBean.setPaginalNumber(count);
        /*当前的页数*/
        postListBean.setCpn(cpn);
        /*列表贴子内容和点赞内容*/
        List<PostBean> list = null;
        {
            list = new ArrayList<>();

            int start = (cpn - 1) * EVERY_PAGE_LIST_SIZE;
            int size = EVERY_PAGE_LIST_SIZE;
            /*获取贴子列表*/
            List<PostEntity> peList = codeDao.selectPostList(codeTypeEntity.getCodeId(),
                    codeTypeEntity.getCodeType(), start, size);

            /*根据贴子id获取点赞列表*/
            for (PostEntity pe : peList)
            {
                List<PostPraiseEntity> ppeList =
                        codeDao.selectPostPraiseListByPostId(pe.getPostId());
                /*获取session中的user实体，判断此用户是否点赞*/
                UserEntity userEntity = (UserEntity) session.getAttribute("user");
                boolean isPraise = false;
                if (userEntity != null)
                    for (PostPraiseEntity entity : ppeList)
                        if (entity.getUserName().equals(userEntity.getUserName()))
                        {
                            isPraise = true;
                            break;
                        }

                list.add(new PostBean(pe, ppeList, isPraise));
            }

        }
        postListBean.setPostList(list);
        return postListBean;
    }
}
